#!usr/bin/env python  
# -*- coding:utf-8 -*-
""" 
@author:robot
@file: title011_2最长的顺子.py 
@version:
@time: 2023/05/27 
"""
graph = [i for i in '3,4,5,6,7,8,9,10,J,Q,K,A'.split(',')]
cards = {}
for card in graph:
    cards[card] = 4


def solve(my, over):
    diff(cards, my)
    diff(cards, over)
    print(cards)
    res = find(cards)
    print(res)


def diff(cards, str_):
    for card in str_.split('-'):
        if card in cards:
            cards[card] -= 1


def find(cards):
    res = "NO-CHAIN"
    l, r = 0, 0
    for i in range(len(graph)):
        card = graph[i]
        if cards[card] > 0:
            l = i
            while i < len(graph) - 3 and cards[graph[i + 1]] > 0:
                i += 1
            r = i + 1
            if r - l >= 5:
                res = "-".join(graph[l:r])
    return res


my = input()
over = input()
solve(my, over)
